from db_method import DataBaseMethod
import requests
import sys
from setting import API_Host, BROKER_ADDRESS, BROKER_PORT
import time
import json
        
from util import send_mqtt_message

eps_list = ["e280f30200000000714d89e5", "e280f30200000000714d8b6d", "e280f30200000000714d8ae5"]

eps_list_06 = ["e280f30200000000714d8b8d", "e280f30200000000714d8b99", "e280f30200000000714d8b95", "e280f30200000000714d8b91", "e280f30200000000714d8ba1"]

eps_list_05 = ["e280f30200000000714d8b89", "e280f30200000000714d8b7d", "e280f30200000000714d8b79", "e280f30200000000714d8b85", "e280f30200000000714d8b81"]

eps_list_04 = ["e280f30200000000714d8bb1", "e280f30200000000714d8ba9", "e280f30200000000714d8bad", "e280f30200000000714d8bb9", "e280f30200000000714d8ba5"]

eps_list_03 = ["e280f30200000000714d8bcd", "e280f30200000000714d8bc1", "e280f30200000000714d8bc9", "e280f30200000000714d8bc5", "e280f30200000000714d8bbd"]

eps_list_02 = ["e280f30200000000714d8bd5", "e280f30200000000714d8bd9", "e280f30200000000714d8be1", "e280f30200000000714d8bd1", "e280f30200000000714d8be5"]

eps_list_01 = ["e280f30200000000714d8bf9", "e280f30200000000714d8bf5", "e280f30200000000714d8bf1", "e280f30200000000714d8bed", "e280f30200000000714d8be9"]
if __name__ == "__main__":

    mac = "863923e31683"

    arg = sys.argv[1]

    if arg == "01":

        DataBaseMethod().insert_door_task({"orderid": str(time.time()), "mac": mac})

        print("添加一个，开门任务")

    if arg == '02':

        url = "http://{}/open_door".format(API_Host)

        data = {"orderid": str(time.time()), "mac": "863923e31683"}

        r = requests.post(url, data=data)

        print(r.json())

    if arg == "03":

        url = "http://{}/insert_esp_tags".format(API_Host)

        r = requests.post(url, data={"tags": json.dumps(eps_list)})
        # r = requests.post(url, data={"tags": eps})

        print(r.json())

    if arg == "04":

        url = "http://{}/get_latest_status_by_mac_list".format(API_Host)

        data = {"mac_list": json.dumps(["863923e31683"])}

        r = requests.post(url, data=data)

        print(r.json())

    if arg == "05":

        url = "http://{}/get_online_status_by_mac_list".format(API_Host)

        data = {"mac_list": json.dumps(["863923e31683", "7ad7555fe25f"])}

        r = requests.post(url, data=data)

        print(r.json())

    if arg == "06":

        url = "http://{}/get_cabinet_heartbeat_by_id".format(API_Host)

        data = {"cabinet_id": 1}

        r = requests.get(url, params=data)

        print(r.text)

    if arg == "08":

        url = "http://{}/open_door______?mac=863923e31683".format(API_Host)

        print(url)



        r = requests.get(url)

        print(r.text)

    if arg == "09":

        url = "http://{}/get_cabinet_abnormal_status_by_mac_list".format(API_Host)

        data = {"mac_list": json.dumps(["863923e31683", "7ad7555fe25f"])}

        r = requests.post(url, data=data)

        print(r.json())

    if arg == "10":

        topic = "rfid_cabinet/rx/{}".format("7ad7555fe25f")
        # topic = "rfid_cabinet/rx/620790426a0e"

        message = {

            "task_type": "restart_auto_ssh"

        }

        # 重启autossh
        send_mqtt_message(broker_address=BROKER_ADDRESS, broker_port=BROKER_PORT, topic=topic, message=json.dumps(message))

    if arg == "11": # 设置广告

        topic = "rfid_cabinet/rx/{}".format("7ad7555fe25f")
        # topic = "rfid_cabinet/rx/620790426a0e"

        message = {

            "task_type": "设置广告",
            "task_data": {
                # "ad_media_url": "http://47.110.239.14:8080/media/rfid_cabinet/one_minute_promotional_video.mp4",
                "ad_media_url": "http://47.110.239.14:8080/media/rfid_cabinet/菁英茶三分钟版本-1080p-1124.mp4",
                "ad_volurm": "1.0"
            }

        }

        # 下发开门命令
        send_mqtt_message(broker_address=BROKER_ADDRESS, broker_port=BROKER_PORT, topic=topic, message=json.dumps(message))

    if arg == "12": # 上传购物视频

        topic = "rfid_cabinet/rx/{}".format("7ad7555fe25f")
        # topic = "rfid_cabinet/rx/620790426a0e"

        message = {

            "task_type": "上传购物视频",
            "task_data": {
                "orderid": "co1706435148184921976"
            }

        }

        # 下发开门命令
        send_mqtt_message(broker_address=BROKER_ADDRESS, broker_port=BROKER_PORT, topic=topic, message=json.dumps(message))

    if arg == "13": # 上传购物视频

        url = "http://{}/send_upload_video_command".format(API_Host)

        data = {
            "orderid": sys.argv[2]
        }

        r = requests.get(url, params=data)

        print(r.json())

    if arg=='14': # get_esp_difference_by_door_orderid

        url = "http://{}/get_esp_difference_by_door_orderid".format(API_Host)

        data = {
            "orderid": sys.argv[2]
        }

        r = requests.get(url, params=data)

        print(r.json())

    if arg=='15': # 升级标签状态

        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_01)
        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_02)
        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_03)
        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_04)
        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_05)
        # DataBaseMethod().test_update_esp_tag_status_by_esp_list(eps_list_06)

        x = ['e28068940000401083c3c0fd', 'e28068940000401083c3c130', 'e28068940000401083c3c0fb', 'e28068940000401083c3c128', 'e28068940000401083c3c135', 'e28068940000401083c3c114', 'e28068940000501083c3c146', 'e28068940000401083c3c144', 'e28068940000401083c3c121', 'e280f30200000000714d8935', 'e28068940000401083c3c112', 'e28068940000401083c3c11b', 'e28068940000401083c3c0f2']

        DataBaseMethod().test_update_esp_tag_status_by_esp_list(x)


    if arg=='16': # 设置广告音量

        topic = "rfid_cabinet/rx/{}".format("7ad7555fe25f")
        # topic = "rfid_cabinet/rx/620790426a0e"

        message = {

            "task_type": "设置广告音量",
            "task_data": {
                "ad_volume": sys.argv[2] # 0-1 的浮点
            }

        }

        # 下发开门命令
        send_mqtt_message(broker_address=BROKER_ADDRESS, broker_port=BROKER_PORT, topic=topic, message=json.dumps(message))

    if arg=='16': # 设置广告音量

        url = "http://{}/send_set_ad_media_volum_command".format(API_Host)

        data = {
            "cabinet_mac": "7ad7555fe25f",
            "ad_volume": sys.argv[2]
        }

        r = requests.get(url, params=data)

        print(r.json())

    if arg=='17': # 设置广告音量

        url = "http://{}/send_set_ad_media_command".format(API_Host)

        data = {
            "cabinet_mac": "7ad7555fe25f",
            "ad_volume": "1.0",
            "ad_media_url": "one_minute_promotional_video.mp4"
        }

        r = requests.get(url, params=data)

        print(r.json())

    if arg=='18': # 下发盘点指令

        topic = "rfid_cabinet/rx/{}".format("7ad7555fe25f")
        # topic = "rfid_cabinet/rx/620790426a0e"

        message = {

            "task_type": "重新盘点",
            "task_data": {
                "orderid": str(time.time())
            }

        }

        # 下发开门命令
        send_mqtt_message(broker_address=BROKER_ADDRESS, broker_port=BROKER_PORT, topic=topic, message=json.dumps(message))

    if arg=='19': # 下发盘点指令

        url = "http://{}/send_inventory_command".format(API_Host)

        data = {
            "cabinet_mac": "7ad7555fe25f",
        }

        r = requests.get(url, params=data)

        print(r.json())

    if arg=='20': # 配色

        url = "http://{}/send_ui_color_command".format(API_Host)
        print(url)

        top_color= "#2c5cb3"
        mid_color= "#3399FF"

        # top_color= "red"
        # mid_color= "#3399FF"

        data = {
            "cabinet_mac": "863923e31683", # 7ad7555fe25f
            # "cabinet_mac": "7ad7555fe25f", # 7ad7555fe25f
            "top_color": "#2c5cb3",
            "mid_color": "#3399FF",
            "title_size": 28,
            "goods_size": 14,
            "bottom_status_size": 18,
            "bottom_total_size": 20,
            "goods_font_color": "red",
            "title_font_color": "white",
            "bottom_status_color": "white",
            "bottom_total_color": "white",

            "top_weight": "2",
            "mid_weight": "6",
            "bottom_weight": "2",
            
        }

        #http://47.110.239.14:8020/send_ui_color_command?cabinet_mac=7ad7555fe25f&top_color=red&mid_color=red

        r = requests.get(url, params=data)

        print(r.request.url)

        print(r.json())